home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / SFGetFolder / sample.c < prev    next >
C/C++ Source or Header  |  1992-06-06  |  4KB  |  78 lines

  1. /*******************************************************************************
  2. * StandardGetFolder Sample Program                             by Ken Kirksey  *
  3. *                                                                              *
  4. *    This little program gives an example of how to use the standard get       *
  5. *    folder function.                                                          *
  6. *                                                                              *
  7. *     Requires:  System 7.0 or later.  ANSI & MacTraps libraries.              *
  8. *******************************************************************************/
  9.  
  10.  
  11. void main()
  12. {
  13.     StandardFileReply       mySFReply;
  14.     Point                   where = {60,60};
  15.     OSErr                   iErr;
  16.     FSSpec                  myFSSpec;
  17.     char                    buffer[80];
  18.     long                    inOutCount;
  19.     int                     testFile;
  20.  
  21.     /*-------------------------------------------------------------------------+
  22.     | Initialize all that toolbox stuff.                                       |
  23.     +-------------------------------------------------------------------------*/
  24.     InitGraf( &thePort );
  25.     InitFonts();
  26.     InitWindows();
  27.     FlushEvents( everyEvent, 0 );
  28.     InitCursor();
  29.     InitMenus();
  30.     TEInit();
  31.     InitDialogs( 0L );
  32.  
  33.     /*-------------------------------------------------------------------------+
  34.     | Call Standard get folder, passing the point you want it to be drawn at,  |
  35.     | the message you want to be displayed above the file list, and a pointer  |
  36.     | to a StandardFileReply record.                                           |
  37.     +-------------------------------------------------------------------------*/
  38.     StandardGetFolder( where, "\pHome Free:", &mySFReply ); 
  39.     
  40.     /*-------------------------------------------------------------------------+
  41.     | Ok, the volume reference number and directory ID of the folder the user  |
  42.     | chose are returned in the sfFile field of the Standard File Reply.  Use  |  
  43.     | use this information to build an FSSpec record that references the file  |
  44.     | you wish to create.                                                      |
  45.     +-------------------------------------------------------------------------*/
  46.     FSMakeFSSpec ( mySFReply.sfFile.vRefNum, 
  47.                    mySFReply.sfFile.parID,
  48.                    "\pMyFile.test",
  49.                    &myFSSpec);
  50.                    
  51.     /*-------------------------------------------------------------------------+
  52.     | Now using the FSSpec record you made, create the file. I've got it set   |
  53.     | to create an Alpha (my favorite text editor) file. Change the creator    |
  54.     | code to suit your taste.                                                 |
  55.     +-------------------------------------------------------------------------*/
  56.     FSpCreate (&myFSSpec, 'ALFA', 'TEXT', mySFReply.sfScript);
  57.  
  58.  
  59.     /*-------------------------------------------------------------------------+
  60.     | Open the file and write a short message to it.                           |
  61.     +-------------------------------------------------------------------------*/
  62.     iErr = FSpOpenDF (&myFSSpec, fsRdWrPerm, &testFile);
  63.     if (iErr != noErr);
  64.     SetEOF (testFile, 0);
  65.         
  66.     inOutCount = sprintf (buffer, "Holey Smokes, It Worked!!!\r");
  67.     iErr = FSWrite (testFile, &inOutCount, buffer); 
  68.     if (iErr != noErr);
  69.     
  70.     /*-------------------------------------------------------------------------+
  71.     | Close the file.                                                          |
  72.     +-------------------------------------------------------------------------*/
  73.     FSClose (testFile);
  74.             
  75. }
  76.  
  77.  
  78.